﻿using UnityEngine;
using Zenject;

public class EnemyHealth : MonoBehaviour
{
    private EnemyBase enemyBase;
    [Inject(Id = "coinPrefab")] private GameObject coinPrefab;

    private void Awake()
    {
        enemyBase = GetComponent<EnemyBase>();
    }

    public void TakeDamage(int damage)
    {
        // TODO 计算最终伤害值
        Debug.Log("Enemy get " + damage + " damage");
        enemyBase.health -= damage;
        if (enemyBase.health <= 0)
        {
            Dead();
        }
    }

    private void Dead()
    {
        enemyBase.isDead = true;
        enemyBase.health = 0;
        RxEventBus.Publish(new EnemyDead(enemyBase.exp));
        Instantiate(coinPrefab, transform.position, Quaternion.identity);
        Destroy(gameObject);
    }
}